//
//  AppDelegate.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/6/24.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "UserAccount.h"
#import "NewFeatureViewController.h"
#import "WelcomeViewController.h"



@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [self setupAppearance];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [self defaultViewController];
    
    [self.window makeKeyAndVisible];
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchViewController:) name:LBSwitchRootVCNotification object:nil];
    
    
    return YES;
}

- (void)switchViewController:(NSNotification *)n
{
    BOOL isMainVC = [n.object intValue];
    MainViewController *mainVC = [[MainViewController alloc]init];
    WelcomeViewController *welVC = [[WelcomeViewController alloc]init];
    self.window.rootViewController = isMainVC ? mainVC : welVC;
}
///定义加载的默认控制器
- (UIViewController *)defaultViewController
{
    if ([UserAccount sharedUserAccount] != nil) {
        //如果登陆判断是否有新版本
        NewFeatureViewController *newVC = [[NewFeatureViewController alloc]init];
        WelcomeViewController *welVC = [[WelcomeViewController alloc]init];
        return [self isNewUpdate] ? newVC : welVC;
    }
    return [[MainViewController alloc]init];
}
//是否是新版本
- (BOOL)isNewUpdate
{
    //1.获取应用程序当前版本
    NSString *currVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    CGFloat version = [currVersion doubleValue];
    NSLog(@"当前版本：%f",version);
    //2.获取应用程序之前版本，从偏好中读取
    NSString *versionKey = @"versionKey";
    CGFloat preVersion = [[NSUserDefaults standardUserDefaults]doubleForKey:versionKey];
    NSLog(@"之前版本：%f",version);
    //3.将当前版本写入偏好设置
    [[NSUserDefaults standardUserDefaults]setDouble:version forKey:versionKey];
    
    //4. 返回
    return version > preVersion;
}




//设置外观
- (void)setupAppearance
{
    [[UINavigationBar appearance]setTintColor:[UIColor orangeColor]];
    [[UITabBar appearance]setTintColor:[UIColor orangeColor]];
}

@end
